package com.springboot.controller.back;

import com.springboot.aop.TokenatureValidation;
import com.springboot.service.UsersinfoService;
import com.springboot.utils.ResultUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户信息控制器
 */
@RestController
@RequestMapping("/UsersBack")
@Slf4j
@Api(tags = "back用户信息")
public class UsersinfoBackController {


    @Resource
    UsersinfoService usersinfoService;

    @PostMapping(value = "/login",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "用户登录", notes = "login")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(name = "code", value = "状态码:10000.失败 20000.成功"),
                    @DynamicParameter(name = "msg", value = "系统返回信息"),
            })
    )
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "username", value = "用户名", required = true),
                        @ApiImplicitParam(paramType = "query", dataType = "String", name = "password", value = "密码", required = true)
    })
    public ResultUtil login(@RequestParam(value = "username", required = true, defaultValue = "") String username,
                            @RequestParam(value = "password", required = true, defaultValue = "") String password) {
        System.out.println(username+password);
        return usersinfoService.login(username,password);
    }

    @GetMapping(value = "/getInfo")
    @ApiOperation(value = "根据token获取用户信息", notes = "getInfo")
    @TokenatureValidation
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(name = "code", value = "状态码:10000.失败 20000.成功"),
                    @DynamicParameter(name = "msg", value = "系统返回信息"),
            })
    )
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "令牌", required = true)})
    public ResultUtil getInfo(@RequestHeader(value = "token", required = true, defaultValue = "") String token) {
        return usersinfoService.getInfo(token);
    }

    @PostMapping(value = "/logout")
    @ApiOperation(value = "退出登录", notes = "logout")
    @TokenatureValidation
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(name = "code", value = "状态码:10000.失败 20000.成功"),
                    @DynamicParameter(name = "msg", value = "系统返回信息"),
            })
    )
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "令牌", required = true)})
    public ResultUtil logout(@RequestHeader(value = "token", required = true, defaultValue = "") String token) {
        return usersinfoService.logout(token);
    }


}
